package test.base.medium;

import org.bytedeco.ffmpeg.avformat.AVFormatContext;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.FrameGrabber.Exception;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_videoio.VideoCapture;
import org.opencv.highgui.HighGui;

import test.base.medium.ffmpeg.Rtsp;

import static org.bytedeco.opencv.global.opencv_highgui.imshow;
import static org.bytedeco.opencv.global.opencv_highgui.waitKey;
import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

public class JavaCVrtsptest3 {
	public static void main(String[] args) {
		// show照片();
		// testRtsp() ;
		// 提取图片() ;
		//rtsp2rtmp();
		rtsp2rtmp2();
	}

	public static void show照片() {
		// 读取原始图片
		Mat image = imread("D:\\zdy\\me\\22.jpg");
		if (image.empty()) {
			System.err.println("加载图片出错，请检查图片路径！");
			return;
		}
		// 显示图片
		imshow("显示原始图像", image);
		// 无限等待按键按下
		waitKey(0);
	}

	private static String rtspurl = "rtsp://admin:nt123321@192.168.0.2:554/h264/ch1/sub/av_stream";
	private static String rtspurl1 = "rtsp://admin:nt123321@192.168.0.2:554/channel=0&stream=0.sdp";

	private static void testRtsp() {

		VideoCapture vc = new VideoCapture();
		boolean isOpen = vc.open(rtspurl);
		System.out.println("isOpen=" + isOpen);
		Mat mat = new Mat();
		String winName = "showFrame";
		int height = 600, width = 800;
		HighGui.namedWindow(winName);
		HighGui.resizeWindow(winName, width, height);

		while (vc.read(mat)) {
			// 重置大小
			// Mat dst = new Mat();
			// Imgproc.resize(mat, dst, new Size(width,height));
			// 显示
			imshow(winName, mat);
			// waitkey 必须要，否则无法显示
			int key = waitKey(0);
			System.out.println("key=" + key);
			if (key == 27) {
				break;
			}
		}
		HighGui.destroyAllWindows();
		//
		vc.release();
	}

	
	static	String rtmpurl="rtmp://192.168.0.147:1935/hyl/test";
	public static void rtsp2rtmp2() {

	
		// 运行，设置视频源和推流地址
		Rtsp rt;
		try {
			 boolean isStart=true;//该变量建议设置为全局控制变量，用于控制录制结束
				// 获取视频源
				FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspurl);
				// 流媒体输出地址，分辨率（长，高），是否录制音频（0:不录制/1:录制）
				FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(rtmpurl, 1280, 720, 1);
				// 开始取视频源
				
					try {
						grabber.start();
						recorder.start();
						Frame frame = null;
						while ( (frame = grabber.grabFrame()) != null) {
							recorder.record(frame);
						}
						recorder.stop();
						grabber.stop();
					} catch (Exception | org.bytedeco.javacv.FrameRecorder.Exception e) {						
						e.printStackTrace();
					}
					
//					new Thread(()->
//					{}).start();

		} catch (java.lang.Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static void do截屏() throws Exception {
		FFmpegFrameGrabber frameGrabber = new FFmpegFrameGrabber(
				new File(System.getProperty("user.dir") + "/test.h264"));

		frameGrabber.setFrameRate(100);
		frameGrabber.setFormat("h264");
		frameGrabber.setVideoBitrate(15);
		frameGrabber.setVideoOption("preset", "ultrafast");
		frameGrabber.setNumBuffers(25000000);

		Java2DFrameConverter converter = new Java2DFrameConverter();

		frameGrabber.start();

		Frame frame = frameGrabber.grab();
		BufferedImage bufferedImage = converter.convert(frame);

	}

	public static void 提取图片() {

		VideoCapture vc = new VideoCapture(rtspurl1);
		boolean isOpen = vc.isOpened(); // vc.open(rtspurl1);
		Mat image = new Mat();
		double propid = 0;

		// capture.open("rtsp://218.204.223.237:554/live/1/66251FC11353191F/e7ooqwcfbqjoo80j.sdp");
		int i = 0;
		if (isOpen) {
			System.out.println("Video is captured");
			int x = 0;
			while (x < 18) {
				x = x + 1;
				propid = vc.get(x);
				System.out.println("Property Id is " + x + " value =" + propid);
			}

			while (i < 10) {

				// capture.retrieve(image); //same results as .read()
				vc.read(image);
				imshow("显示原始图像", image);
				System.out.println("read a frame at " + i);

				i = i + 1;

			}
		} else {
			System.out.println("Camera can not be opened!");
		}
		vc.release();
		System.out.println("VideoCapture is released");
		System.exit(0);
	}
	



}
